package com.example.myapplication;

import com.blankj.utilcode.util.LogUtils;
import com.google.gson.Gson;

import org.jetbrains.annotations.NotNull;

import java.io.IOException;
import java.util.Iterator;
import java.util.LinkedHashMap;

import kotlin.Pair;
import okhttp3.FormBody;
import okhttp3.Interceptor;
import okhttp3.MultipartBody;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;

/**
 * crated by Administrator on 2021/5/31
 * 746608431@qq.com
 * 描述：TODO
 */
public class EncryptInterceptor implements Interceptor {
    public static String TAG = "EncryptInterceptor===========";

    @NotNull
    @Override
    public Response intercept(@NotNull Chain chain) throws IOException {
        Request originalRequest = chain.request();
        String method = originalRequest.method();
        RequestBody newRequestBody = null;
        if ("POST".equals(method)) {


            RequestBody originalRequestBody = originalRequest.body();

            if (originalRequestBody != null) {


                if (originalRequestBody instanceof FormBody) { // 传统表单

                    FormBody.Builder builder = new FormBody.Builder();
                    FormBody requestBody = (FormBody) originalRequest.body();
                    int fieldSize = requestBody == null ? 0 : requestBody.size();

                    LinkedHashMap<String, Object> hashMap = new LinkedHashMap<>();

                    for (int i = 0; i < fieldSize; i++) {
                        hashMap.put(requestBody.encodedName(i), requestBody.encodedValue(i));
                    }
                    LogUtils.d("加密前的info===", new Gson().toJson(hashMap));

                    String info = null;
                    try {
                        info = DES3Utils.encode(new Gson().toJson(hashMap));
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                    LogUtils.d("加密后的info===", info);
                    builder.add("canshu", info);


                    newRequestBody = builder.build();

                    Request.Builder requestBuilder = originalRequest.newBuilder().method(originalRequest.method(), newRequestBody);

                    return chain.proceed(requestBuilder.build());

                } else if (originalRequestBody instanceof MultipartBody) { // 文件
                    MultipartBody requestBody = (MultipartBody) originalRequest.body();
                    MultipartBody.Builder multipartBodybuilder = new MultipartBody.Builder();


                    for (int i = 0; i < requestBody.parts().size(); i++) {
                        MultipartBody.Part part = requestBody.part(i);
                        multipartBodybuilder.addPart(part);
                        Iterator<Pair<String, String>> iterator = part.headers().iterator();
                        while (iterator.hasNext()){
                            iterator.next();
                            LogUtils.d(" iterator.next()=====", iterator.next());
                        }

                        LogUtils.d("part======", part);
                    }


//                    MultipartBody multipartBody = (MultipartBody) request.body();
//                    List<MultipartBody.Part> parts=multipartBody.parts();
//
//                    for (MultipartBody.Part part:parts) {
//                        if(part.body().contentType().type().equals("text")){
//                            Headers headers=part.headers();
//                            for (int i = 0; i <headers.names().size() ; i++) {
//                                Log.d("ParameterInterceptor","headers======value="+headers.value(i));
//                                String value=headers.value(i);//valueform-data; name="article_type"
//                                String replaceValue="form-data; name=";//这段在MultipartBody.Part源码中看到
//                                if(value.contains(replaceValue)){
//                                    String key=value.replace(replaceValue,"").replaceAll("\"","");;
//                                    Log.d("ParameterInterceptor","MultipartBody======key="+key);
//                                    map.put(key,bodyToString(part.body()));
//                                    builder.addPart(part);
//                                    break;
//                                }
//                            }
//                        }
//                        Log.d("ParameterInterceptor","MultipartBody======"+bodyToString(part.body()));
//                        Log.d("ParameterInterceptor","MultipartBody======type=="+part.body().contentType().type());
//                    }

                }

            }

        } else if ("GET".equals(method)) {

        }


        return chain.proceed(originalRequest);
    }
}
